package mobi.springtour;

import java.io.IOException;

import mobi.springtour.R;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NetActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.net);
			getJsonDataFromLocalhost();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getJsonDataFromLocalhost() {
		TextView showJsonData = (TextView) findViewById(R.id.showJsonData);
		String httpUrl = "http://192.168.9.84:8080/actions/News.action";
		HttpGet httpRequest = new HttpGet(httpUrl);

		try {
			HttpClient client = new DefaultHttpClient();
			HttpResponse httpResponse = client.execute(httpRequest);

			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 取得json值,转装成对象
				String jsonData = EntityUtils
						.toString(httpResponse.getEntity());
				showJsonData.setText(jsonData);
			} else {
				showJsonData.setText("获取数据错误!");
			}
		} catch (ClientProtocolException e) {
			showJsonData.setText(e.getMessage().toString());
			e.printStackTrace();
		} catch (IOException e) {
			showJsonData.setText(e.getMessage().toString());
			e.printStackTrace();
		} catch (Exception e) {
			showJsonData.setText(e.getMessage().toString());
		}
	}
}
